package cn.zt.service.impl;

import cn.zt.common.Result;
import cn.zt.domain.*;
import cn.zt.domain.vo.RechargeVo;
import cn.zt.domain.vo.TransferVo;
import cn.zt.mapper.BankcardMapper;
import cn.zt.service.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zt.mapper.BalanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import java.util.List;


/**
* @author 86182
* @description 针对表【balance(余额表
)】的数据库操作Service实现
* @createDate 2024-09-03 19:10:48
*/
@Service
public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance>
    implements BalanceService {
    @Autowired
    private BankcardService bankcardService;
    @Autowired
    private BankCardTypeService bankCardTypeService;
    @Autowired
    private BalanceService balanceService;
    @Autowired
    private TClueService tclueService;
    @Autowired
    private BankTypeService bankTypeService;

    //转账
    @Transactional
    @Override
    public Result transfer(TransferVo transferVo) {
        //        判断收款账号是否存在
        Bankcard bankcard = bankcardService.selectByBankCard(transferVo.getBankCard());
//        if (!(bankcard.getNickname().equals(transferVo.getBankCard()))){
//            return  Result.error("账号和开户行是否匹配");
//        }
        if (bankcard == null) {
            return Result.error("收款账号不存在");
        }
        //收款人姓名和银行卡号是否一致
        List<Bankcard> bankcards = bankcardService.selectByBankCardname(transferVo.getUsername());
        for (Bankcard bankcard1 : bankcards) {
            if (bankcard1.getBankCard().equals(transferVo.getBankCard())) {
                //判断银行卡号和开户行是否一致
                BankType bankType = bankTypeService.selectByBankCard(transferVo.getBankCard());
                if (bankType != null && bankType.getTypeName() != null ){
                    if (!(bankType.getTypeName().equals(transferVo.getNickname()))){
                        return Result.error("银行卡号和开户行不匹配");
                    }
                }


                //        异行转账金金额低于5000元不需要手续费，在5000-10000区间收取5元/笔，
//        1万-5万7.5元/笔，5万以上按0.015%收取，25封顶
                //根据银行卡号判断是否为同行转账
                BankCardType bankCardType = bankCardTypeService.selectByBankCard(transferVo.getBankCard());
                BankCardType bankCardType1 = bankCardTypeService.selectByBankCard(transferVo.getFuBankCard());

                //判断支付密码密码   根据付款银行卡号查找支付密码
                Bankcard bankcard2 = bankcardService.getOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, transferVo.getFuBankCard()));
                if (!(bankcard2.getPayPassword().equals(transferVo.getPaypassword()))) {
                    return Result.error("支付密码错误");
                }


                //同行转账  不收取手续费
                if (bankCardType != null && bankCardType1 != null && bankCardType.getBankTypeId().equals(bankCardType1.getBankTypeId())) {
                    //收款方
                    Bankcard shoukuan = balanceService.selectbalance(transferVo.getBankCard());
                    if (shoukuan != null && shoukuan.getBalance() != null) {
                        bankcardService.updateBalance(transferVo.getBankCard(), shoukuan.getBalance() + transferVo.getMoney());
                    }
                    //付款方
                    Bankcard fukuan = balanceService.selectbalance(transferVo.getFuBankCard());
                    if (fukuan.getBalance() < transferVo.getMoney() && (fukuan.getBalance() - transferVo.getMoney()) < 0) {
                        return Result.error("余额不足,不能转账");
                    }
                    int i = bankcardService.updateBalance(transferVo.getFuBankCard(), fukuan.getBalance() - transferVo.getMoney());
                    if (i > 0) {
                        //添加一条转账记录
                        TClue tClue = new TClue();
                        //登录人是老乡
                        tClue.setCreateBy(62);
                        tClue.setCreateTime(new Date());
                        tClue.setNoteWay("转账");
                        tClue.setNoteContent(fukuan.getNickname()+"向"+shoukuan.getNickname()+"转账"+transferVo.getMoney()+"元");
                        tclueService.save(tClue);
                    }
                    return i > 0 ? Result.success("转账成功", 0) : Result.error("转账失败");
                } else {
                    //异行转账 收取手续费
//            低于5000元不需要手续费
                    if (transferVo.getMoney() < 5000) {
                        //收款方
                        Bankcard shoukuan = balanceService.selectbalance(transferVo.getBankCard());
                        if (shoukuan != null && shoukuan.getBalance() != null) {
                            bankcardService.updateBalance(transferVo.getBankCard(), shoukuan.getBalance() + transferVo.getMoney());
                        }
                        //付款方
                        Bankcard fukuan = balanceService.selectbalance(transferVo.getFuBankCard());
                        if (fukuan.getBalance() < transferVo.getMoney() && (fukuan.getBalance() - transferVo.getMoney()) < 0) {
                            return Result.error("余额不足,不能转账");
                        }
                        int i = bankcardService.updateBalance(transferVo.getFuBankCard(), (fukuan.getBalance() - transferVo.getMoney()));
                        if (i > 0) {
                            //添加一条转账记录
                            TClue tClue = new TClue();
                            //登录人是老乡
                            tClue.setCreateBy(62);
                            tClue.setCreateTime(new Date());
                            tClue.setNoteWay("转账");
                            tClue.setNoteContent(fukuan.getNickname()+"向"+shoukuan.getNickname()+"转账"+transferVo.getMoney()+"元");
                            tclueService.save(tClue);
                        }
                        return i > 0 ? Result.success("转账成功", 0) : Result.error("转账失败");
//                 在5000-10000区间收取5元/笔，
                    } else if (transferVo.getMoney() >= 5000 && transferVo.getMoney() <= 10000) {
                        //收款方
                        Bankcard shoukuan = balanceService.selectbalance(transferVo.getBankCard());
                        if (shoukuan != null && shoukuan.getBalance() != null) {
                            bankcardService.updateBalance(transferVo.getBankCard(), shoukuan.getBalance() + transferVo.getMoney());
                        }
                        //付款方
                        Bankcard fukuan = balanceService.selectbalance(transferVo.getFuBankCard());
                        if (fukuan.getBalance() < transferVo.getMoney() && (fukuan.getBalance() - transferVo.getMoney() - 5) < 0) {
                            return Result.error("余额不足,不能转账");
                        }
                        int m1 = transferVo.getMoney() + 5;
                        System.out.println(m1 + "111111111111111111111111111111111111111111111111111111111111111111");
                        int i = bankcardService.updateBalance(transferVo.getFuBankCard(), (fukuan.getBalance() - m1));
                        if (i > 0) {
                            //添加一条转账记录
                            TClue tClue = new TClue();
                            //登录人是老乡
                            tClue.setCreateBy(62);
                            tClue.setCreateTime(new Date());
                            tClue.setNoteWay("转账");
                            tClue.setNoteContent(fukuan.getNickname()+"向"+shoukuan.getNickname()+"转账"+transferVo.getMoney()+"元");
                            tclueService.save(tClue);
                        }
                        return i > 0 ? Result.success("转账成功", 5) : Result.error("转账失败");
                    }
//         1万-5万  7.5元/笔
                    else if (transferVo.getMoney() >= 10000 && transferVo.getMoney() <= 50000) {
                        //收款方
                        Bankcard shoukuan = balanceService.selectbalance(transferVo.getBankCard());
                        if (shoukuan != null && shoukuan.getBalance() != null) {
                            bankcardService.updateBalance(transferVo.getBankCard(), (shoukuan.getBalance() + transferVo.getMoney()));
                        }
                        //付款方
                        Bankcard fukuan = balanceService.selectbalance(transferVo.getFuBankCard());
                        if (fukuan.getBalance() < transferVo.getMoney() && (fukuan.getBalance() - transferVo.getMoney() - 7.5) < 0) {
                            return Result.error("余额不足,不能转账");
                        }
                        int i = bankcardService.updateBalance(transferVo.getFuBankCard(), (int) (fukuan.getBalance() - transferVo.getMoney() - 7.5));
                        if (i > 0) {
                            //添加一条转账记录
                            TClue tClue = new TClue();
                            //登录人是老乡
                            tClue.setCreateBy(62);
                            tClue.setCreateTime(new Date());
                            tClue.setNoteWay("转账");
                            tClue.setNoteContent(fukuan.getNickname()+"向"+shoukuan.getNickname()+"转账"+transferVo.getMoney()+"元");
                            tclueService.save(tClue);
                        }
                        return i > 0 ? Result.success("转账成功", 7.5) : Result.error("转账失败");
                    }
            //        5万以上按0.015%收取，25封顶
                    else if (transferVo.getMoney() > 50000) {
                        //收款方
                        Bankcard shoukuan = balanceService.selectbalance(transferVo.getBankCard());
                        if (shoukuan != null && shoukuan.getBalance() != null) {
                            bankcardService.updateBalance(transferVo.getBankCard(), shoukuan.getBalance() + transferVo.getMoney());
                        }
                        //付款方
                        Bankcard fukuan = balanceService.selectbalance(transferVo.getFuBankCard());
                        int i = (int) (7.5 + (transferVo.getMoney() - 50000) * 0.015);
                        if (i > 25) {
                            i = 25;
                        }
                        if (fukuan.getBalance() < transferVo.getMoney() && (fukuan.getBalance() - transferVo.getMoney() - i) < 0) {
                            return Result.error("余额不足,不能转账");
                        }
                        int i1 = bankcardService.updateBalance(transferVo.getFuBankCard(), (fukuan.getBalance() - transferVo.getMoney() - i));
                        if (i1 > 0) {
                            //添加一条转账记录
                            TClue tClue = new TClue();
                            //登录人是老乡
                            tClue.setCreateBy(62);
                            tClue.setCreateTime(new Date());
                            tClue.setNoteWay("转账");
                            tClue.setNoteContent(fukuan.getNickname()+"向"+shoukuan.getNickname()+"转账"+transferVo.getMoney()+"元");
                            tclueService.save(tClue);

                        }
                        return i1 > 0 ? Result.success("转账成功", i) : Result.error("转账失败");
                    }
                }

            }
        }





        return Result.error("转账失败");
    }

    //卡号对应的查余额
    @Override
    public Result selectBalanceByBankCard(String bankCard) {
        Bankcard bankcard = bankcardService.getOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, bankCard));
        if (bankcard.getId() != null) {
            BankCardType bankCardType = bankCardTypeService.getOne(Wrappers.<BankCardType>lambdaQuery()
                    .eq(BankCardType::getBankcardId, bankcard.getId()));

            if (bankCardType.getBalanceId() != null) {
                Balance balance = balanceService.getOne(Wrappers.<Balance>lambdaQuery()
                        .eq(Balance::getId, bankCardType.getBalanceId()));
                if (balance != null) {
                    bankcard.setBalance(balance.getBalanceNum());
                }
            }
        }
        return Result.success("查询成功", bankcard);
    }

    @Override
    public int addBalance(Balance balance) {
        this.save(balance);
        return balance.getId();

    }

    @Override
    public Bankcard selectbalance(String bankCard) {
        Bankcard bankcard = bankcardService.getOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, bankCard));
        if (bankcard != null && bankcard.getId() != null) {
            BankCardType bankCardType = bankCardTypeService.getOne(Wrappers.<BankCardType>lambdaQuery()
                    .eq(BankCardType::getBankcardId, bankcard.getId()));
            if (bankCardType != null && bankCardType.getBalanceId() != null) {
                Balance balance = balanceService.getOne(Wrappers.<Balance>lambdaQuery()
                        .eq(Balance::getId, bankCardType.getBalanceId()));
                if (balance != null) {
                    bankcard.setBalance(balance.getBalanceNum());
                }
            }
        }
        return bankcard;
    }

    //    充值
    @Override
    public Result addMoney(RechargeVo rechargeVo) {
        //查询银行卡以及余额
        Bankcard bankcard = balanceService.selectbalance(rechargeVo.getBankcard());
        if (bankcard != null) {
            if (rechargeVo.getMoney() < 0) {
                return Result.error("充值失败,充值金额应大于0￥");
            }
//        支付密码
            //判断支付密码密码   根据付款银行卡号查找支付密码
            Bankcard bankcard2 = bankcardService.getOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, rechargeVo.getBankcard()));
            if (bankcard2 == null) {
                return Result.error("银行卡号不存在");
            }
            if (rechargeVo.getPaypassword() == null) {
                return Result.error("请输入支付密码");
            }
            if (!(bankcard2.getPayPassword().equals(rechargeVo.getPaypassword()))) {
                return Result.error("支付密码错误");
            }

            if (bankcard != null && bankcard.getBalance() != null && rechargeVo.getMoney() > 0) {
                int i = bankcardService.updateBalance(bankcard.getBankCard(), bankcard.getBalance() + rechargeVo.getMoney());
                if (i > 0) {
                    //添加一条充值记录
                    TClue tClue = new TClue();
                    //登录人是老乡
                    tClue.setCreateBy(62);
                    tClue.setCreateTime(new Date());
                    tClue.setNoteWay("充值");
                    tClue.setNoteContent(bankcard.getNickname()+"的"+bankcard.getBankCard()+"充值"+rechargeVo.getMoney()+"元");
                    tclueService.save(tClue);
                }
                return i > 0 ? Result.success("充值成功") : Result.error("充值失败");
            }

        }
        return Result.error("充值失败");
    }

    //提现
    @Override
    public Result withdrawals(RechargeVo rechargeVo) {
        Bankcard bankcard = balanceService.selectbalance(rechargeVo.getBankcard());
        if (bankcard != null) {
            if (rechargeVo.getMoney() < 0) {
                return Result.error("充值失败,充值金额应大于0￥");
            }

//        支付密码
            //判断支付密码密码   根据付款银行卡号查找支付密码
            Bankcard bankcard2 = bankcardService.getOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, rechargeVo.getBankcard()));
            if (bankcard2 == null) {
                return Result.error("银行卡号不存在");
            }
            if (rechargeVo.getPaypassword() == null) {
                return Result.error("请输入支付密码");
            }
            if (!(bankcard2.getPayPassword().equals(rechargeVo.getPaypassword()))) {
                return Result.error("支付密码错误");
            }

            if (rechargeVo.getMoney() > 10000){
                return Result.error("提现失败，单日限额一万");
            }
            if (bankcard != null && bankcard.getBalance() != null && rechargeVo.getMoney() > 0) {
                int i = bankcardService.updateBalance(bankcard.getBankCard(), bankcard.getBalance() - rechargeVo.getMoney());
                if (i > 0) {
                    //添加一条提现记录
                    TClue tClue = new TClue();
                    //登录人是老乡
                    tClue.setCreateBy(62);
                    tClue.setCreateTime(new Date());
                    tClue.setNoteWay("提现");
                    tClue.setNoteContent(bankcard.getNickname()+"的"+bankcard.getBankCard()+"提现"+rechargeVo.getMoney()+"元");
                    tclueService.save(tClue);
                }
                return i > 0 ? Result.success("提现成功") : Result.error("提现失败");
            }

        }
       return Result.error("提现失败");
    }

    @Override
    public Result CreditToBank(Balance balance) {
        Integer id = bankcardService.findId(balance.getBankCard());
        Balance one = this.getOne(Wrappers.<Balance>lambdaQuery().eq(Balance::getId, id));
        balance.setId(id);
        balance.setBalanceNum((int) (one.getBalanceNum()+balance.getNum()));
        balance.setBalanceNum((int) (one.getBalanceNum() * 0.9  + balance.getNum()));
        boolean b = this.updateById(balance);
        return b?Result.success("转入成功"):Result.error("转入失败");
    }

    @Override
    public Result BankToCredit(Balance balance) {
        Integer id = bankcardService.findId(balance.getBankCard());
        Balance one = this.getOne(Wrappers.<Balance>lambdaQuery().eq(Balance::getId, id));
        if (one.getBalanceNum()>=balance.getNum()){
            balance.setId(id);
            balance.setBalanceNum((int) (one.getBalanceNum()-balance.getNum()));
            boolean b = this.updateById(balance);
            return b?Result.success("转出成功"):Result.error("转出失败");
        }
        return Result.warn("余额不足");
    }

}



   






